package dao.impl;

import dao.BaseDao;
import dao.Checkout_recordsDao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Checkout_recordsDaoimpl extends BaseDao implements Checkout_recordsDao {
    @Override
    public int add(String student_id, String checkout_type, String detail) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement pre = null;

        try {

            String sql = "INSERT INTO `student`.`checkout_records` (`student_id`, `operator`, `checkout_type`, `detail`) " +
                    "VALUES (?, 'dormitory', ?, ?)";
            pre = conn.prepareStatement(sql);
            pre.setString(1, student_id);
            pre.setString(2, checkout_type);
            pre.setString(3, detail);

            return pre.executeUpdate();
        } finally {
            // 关闭PreparedStatement
            if (pre != null) {
                try {
                    pre.close();
                } catch (SQLException e) {
                    e.printStackTrace(); // 实际项目中建议使用日志框架
                }
            }
            // 关闭Connection
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace(); // 实际项目中建议使用日志框架
                }
            }
        }
    }
}
